package com.huike.sideslip;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 自定义侧滑
 */
public class SideSlipLayout extends FrameLayout {

    private TextView tv_la;
    private TextView tv_item;
    private int tv_laMeasuredWidth;
    private int tv_itemMeasuredWidth;
    private int height;
    private TextView tv_item2;
    private int tv_item2MeasuredWidth;



    public SideSlipLayout(@NonNull Context context) {
        this(context,null);
    }

    public SideSlipLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public SideSlipLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


//    布局加载完成回调
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        //得到视图（因为没加载视图所以所有这种方式找到id）
        tv_la = (TextView) getChildAt(0);
        tv_item = (TextView) getChildAt(1);
        tv_item2 = (TextView) getChildAt(2);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量得到 视图 w h
        tv_laMeasuredWidth = tv_la.getMeasuredWidth();
        tv_itemMeasuredWidth = tv_item.getMeasuredWidth();
        tv_item2MeasuredWidth = tv_item2.getMeasuredWidth();
        height = getMeasuredHeight();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    //隐藏布局
        tv_item.layout(tv_laMeasuredWidth,0,tv_laMeasuredWidth+tv_itemMeasuredWidth,height);
        tv_item2.layout(tv_itemMeasuredWidth+tv_laMeasuredWidth,0,tv_laMeasuredWidth+tv_itemMeasuredWidth+tv_item2MeasuredWidth,height);
    }

    private   float startX;
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:

                  startX = event.getX();

                  if(lisener != null){
                      lisener.onDow(this);
                  }


                break;
            case MotionEvent.ACTION_MOVE:

                float endX = event.getX();
                float dy = endX - startX;//偏移量

                //移动
                int toScrollX = (int) (getScrollX()  - dy);
                if(toScrollX < 0){
                    toScrollX = 0;
                }else if(toScrollX > tv_itemMeasuredWidth + tv_item2MeasuredWidth){
                    toScrollX = tv_itemMeasuredWidth + tv_item2MeasuredWidth;
                }

                scrollTo(toScrollX,getScrollY());

                startX = event.getX();

                getParent().requestDisallowInterceptTouchEvent(true);//拦截listview 事件(防止侧滑卡住)

                break;
            case MotionEvent.ACTION_UP:


                int scrollX = getScrollX();

                if(scrollX <= tv_itemMeasuredWidth ){
                    //关闭
                    closeItem();

                }else{
                    //打开
                        openItem();
                }


                break;
        }

        return true;
    }

    public void openItem() {
        scrollTo(tv_item2MeasuredWidth+tv_itemMeasuredWidth,getScrollY());
        if(lisener != null){
            lisener.onOpen(this);
        }
    }

    public void closeItem() {
        scrollTo(0,getScrollY());
        if(lisener != null){
            lisener.onClose(this);
        }

    }

    //使用接口 回调 只能打开一个
    private onStateChangeLisener lisener;
    public void setOnStateChangeLisener(onStateChangeLisener lisener){
        this.lisener = lisener;
    }

    public interface onStateChangeLisener{
        void onDow(SideSlipLayout layout);
        void onClose(SideSlipLayout layout);
        void onOpen(SideSlipLayout layout);
    }

}
